/**
 * Created by caosh on 2018/1/22 0022.
 */
var OftenBuy = new Vue({
    el: '#often-buy',
    data: {
        navBarUlNums: [],
        navBarSelected: '全部',
        num: 1,
        pId: 0,
        product: {},
        isShowMask: false,
        isShowCart: false,
        cartBarTip: '编辑'
    },
    methods: {
        // 显示选中的分类
        showCategorie: function (categorie) {
            this.navBarSelected = categorie;
            if (categorie != '全部') {
                this.getProductsByCategorie(this.oftenBuyList, categorie);
            } else {
                this.searchResults = this.oftenBuyList;
            }
        },
        initProduct: function (p, n) {
            this.product = this.getProductFromPool(p, n);
            this.product.unitPrice = n.price;
            this.product.unit = n.unit;
            this.product.nId = n.id;
            this.num = this.product.num;
        },
        confirmProduct: function (NO) {
            this.product.num = NO;
            if (NO > 0 && this.cartProducts.indexOf(this.product) == -1) {
                this.cartProducts.push(this.product);
            }

            this.triggerRemoveProduct(NO, this.product);
        },
        clickReduce: function (p, n) {
            this.numObj[p.id+'-'+n.id]--;
            this.confirmProduct(this.numObj[p.id+'-'+n.id]);
        },
        clickPlus: function (p, n) {
            if (!p.hasOwnProperty('num')) {
                Vue.set(p, 'num', 1);
            }

            if (!n.hasOwnProperty('showControl')) {
                Vue.set(n, 'showControl', true);
            }

            this.initProduct(p, n);

            var NO = this.numObj[p.id+'-'+n.id];
            if (!NO && NO != 0) {
                this.numObj[p.id+'-'+n.id] = this.num;
            } else {
                this.numObj[p.id+'-'+n.id]++;
            }

            this.confirmProduct(this.numObj[p.id+'-'+n.id]);

            // this.changeMask(p, n);
            n.click = !n.click;
        },
        /*clickPlus: function (p, n) {
            if (!p.hasOwnProperty('num')) {
                Vue.set(p, 'num', 1);
            }
            this.changeMask(p, n);
            n.click = !n.click;
        },*/
        // 隐藏 / 显示 遮罩层
        changeMask: function (p, n) {
            this.isShowMask = !this.isShowMask;
            if (this.isShowMask) {
                /*this.product = this.getProductFromPool(p, n);
                this.product.unitPrice = n.price;
                this.product.unit = n.unit;
                this.product.nId = n.id;
                this.num = this.product.num;*/
                this.initProduct(p, n);
                this.pId = p.id;
            } else {
                this.pId = 0;
            }
        },
        // 取消
        cancel: function () {
            this.num = this.product.num;
            this.changeMask();
        },
        // 确定
        confirm: function () {
            /*this.product.num = this.num;
            if (this.cartProducts.indexOf(this.product) == -1) {
                this.cartProducts.push(this.product);
            }*/
            this.confirmProduct(this.num);
            this.numObj[this.product.id+'-'+this.product.nId] = this.num;
            // 本地缓存购物车数据
            // this.cacheCartProducts();
            this.changeMask();
        },
        // 加
        plus: function () {
            this.num++;
        },
        // 减
        reduce: function () {
            if (this.num>0) {
                this.num--;
            }

            // this.triggerRemoveProduct(this.num, this.product);
        }
    },
    mixins: [ProductMixin, CartMixin, SystemMixin],
    mounted: function () {
        // 分类所占栏目数
        for(var i=0; i<Math.ceil(this.productCategories.length/5); i++) {
            this.navBarUlNums.push(i);
        }

        // 获得常购清单数据
        this.getOftenBuyList();
        // 获得购物车数据
        this.getCartProducts();
        // 获取全部常购清单数据
        this.showCategorie(this.navBarSelected);
    },
    watch: {
        num: function (val, oldVal) {
            if (val <= 0 && val != '') {
                this.num = 1;
            }
        }
    }
});